#include "hostlist.h"
#include <QDebug>

using namespace Moolink;

HostList::HostList(QObject *parent) :
    QAbstractListModel(parent)
{
    m_display_list.clear();
    m_roleNames.insert(RoleName, "name");
    m_roleNames.insert(RoleHostIp, "hostIp");
    m_roleNames.insert(RoleHostPort, "hostPort");
    m_roleNames.insert(RoleProtocol, "protocol");
    m_roleNames.insert(RolecolorCode, "colorCode");
    m_roleNames.insert(RoledataString, "dataString");


    //从数据库初始化设备参数
//    m_display_list.push_back(Device("sensor01","COM10","A",1));
//    m_display_list.push_back(Device("sensor02","COM10","A",2));
//    m_display_list.push_back(Device("sensor03","COM10","A",3));
//    m_display_list.push_back(Device("sensor04","COM10","A",4));
//    m_display_list.push_back(Device("sensor05","COM10","A",5));
//    m_display_list.push_back(Device("sensor06","COM10","A",6));
//    m_display_list.push_back(Device("sensor07","COM10","A",7));

}


int HostList::rowCount(const QModelIndex &parent) const
{
    return m_display_list.size();
}

int HostList::rowCountRead()
{
    return m_display_list.size();
}


QVariant HostList::data(const QModelIndex &index, int role) const
{
    Host ele = m_display_list.at(index.row());
    QVariant var;
    switch (role) {
//    RoleName
//    RolePortname,
//    RoleProtocol,
//    RoleCommaddr,
//    RolecolorCode
    case RoleName:
        var = ele.HostName;
        break;
    case RoleHostIp:
        var = ele.HostIp;
        break;
    case RoleHostPort:
        var = ele.HostPort;
        break;
    case RoleProtocol:
        var = ele.protocol;
        break;
    case RolecolorCode:
        var = ele.colorCode;
        break;
    case RoledataString:
        var = ele.dataString;
        break;
    default:
        break;
    }

    return var;
}


QHash<int, QByteArray> HostList::roleNames() const
{
    return m_roleNames;
}

void HostList::changeName(int index)
{
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    beginResetModel();
    m_display_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();

}
int HostList::getdataIndexById(QString serverId)
{

    for(int i=0;i<m_display_list.size();i++)
    {

        if(m_display_list[i].id==serverId) return i;
    }

}



void HostList::clickItem(int index)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    beginResetModel();
    m_display_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();
}

void HostList::changeDataString(int index,QString str,int error)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    beginResetModel();
    if(0==error) m_display_list[index].colorCode="green"; else m_display_list[index].colorCode="red";
    m_display_list[index].dataString=str;
    endResetModel();
}


//void HostList::addHost(QString name_, QString hostip_, QString hostprot_, QString protocol_)
//{
//    beginInsertRows(QModelIndex(), m_display_list.count(), m_display_list.count());
//    m_display_list.push_back(Host(name_, hostip_, hostprot_.toInt(), protocol_));
//    endInsertRows();
//    qDebug()<<"addServer Ok,Now ListCount="<<m_display_list.size();
//    //emit dataChanged();
//}

void HostList::addHost(QVariant hostVar)
{
    beginInsertRows(QModelIndex(), m_display_list.count(), m_display_list.count());
    m_display_list.push_back(Host(hostVar));
    endInsertRows();
    qDebug()<<"addServer Ok,Now ListCount="<<m_display_list.size();
}
